﻿using HelloOne.NewsSvc.Models;
using HelloOne.NewsSvc.ViewModels;
using HelloOne.Repositories;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;

namespace HelloOne.NewsSvc.Controllers
{
    /// <summary>
    /// 菜单
    /// </summary>
    [Produces("application/json")]
    [Route("api/Menu")]
    public class MenuController : Controller
    {
        private IRepository<Menu> _repository;
        private IRepository<MenuCategory> _menuCategoryRepository;
        public MenuController(IRepositoryProvider provider)
        {
            _repository = provider.Create<Menu>();
            _menuCategoryRepository = provider.Create<MenuCategory>();
        }
        // GET: api/Menu
        [HttpGet]
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET: api/Menu/5
        [HttpGet("{id}", Name = "Get")]
        public string Get(int id)
        {
            return "value";
        }

        // POST: api/Menu
        [HttpPost]
        public void Post([FromBody]MenuVM vm)
        {
            var menu = new Menu(vm.Name, vm.Sort);
            if (!string.IsNullOrWhiteSpace(vm.ParentID))
            {
                //获得上级菜单信息
                var parentMenu = _repository.FindSingle(x => x.ID ==Guid.Parse(vm.ParentID));
                menu.ParentMenu = parentMenu;
            }
            //TODO 保存Menu
            this._repository.Save(menu);

            var category = new MenuCategory(menu.ID,vm.CategoryEnum,vm.CategoryValue);
            this._menuCategoryRepository.Save(category);
        }

        // PUT: api/Menu/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE: api/ApiWithActions/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
    }
}
